package jsonpb

import (
	"io"

	"gitee.com/xuyiping_admin/pkg/xerr"

	"github.com/golang/protobuf/jsonpb"
	"github.com/golang/protobuf/proto"
)

var unmarshaler = &jsonpb.Unmarshaler{AllowUnknownFields: true}

func Unmarshal(r io.Reader, pb proto.Message) (err error) {
	defer func() {
		if e := recover(); e != nil {
			err = e.(error)
		}
	}()

	if err := unmarshaler.Unmarshal(r, pb); err != nil {
		return xerr.WithStack(err)
	}
	return nil
}

// Unmarshaler jsonpb unmarshaler
type Unmarshaler interface {
	Unmarshal(r io.Reader, pb proto.Message) error
}
